import { createRouter, createWebHistory, VueRouter } from 'vue-router'
import store from '@/store'
import { notification } from 'ant-design-vue'
import NProgress from 'nprogress' // 页面加载进度条
import 'nprogress/nprogress.css'

// prettier-ignore
NProgress.configure({
    easing: 'ease',         // 动画方式
    speed: 500,             // 递增进度条的速度
    showSpinner: false,     // 是否显示加载ico
    trickleSpeed: 200,      // 自动递增间隔
    minimum: 0.3,           // 初始化时的最小百分比
})

/**
 * 路由表
 * @type {[{path: string, component: (function(): Promise<*>), hidden: boolean, meta: {title: string}, name: string}, {path: string, component: (function(): Promise<*>), hidden: boolean, meta: {title: string}, name: string}, {path: string, component: (function(): Promise<*>), hidden: boolean, meta: {title: string}, name: string}, {path: string, component: (function(): Promise<*>)}]}
 */

const routes = [
    {
        path: '/test',
        name: 'test',
        component: () => import('@/components/test'),
        meta: { title: '测试' },
        hidden: true,
    },
    {
        path: '/demo',
        name: 'demo',
        component: () => import('@/components/demo'),
        meta: { title: 'demo' },
        hidden: true,
    },
    {
        path: '/user_center_demo',
        name: 'user_center_demo',
        component: () => import('@/components/user_center_demo'),
        meta: { title: '个人中心' },
        hidden: true,
    },
    {
        path: '/author_show_demo',
        name: 'author_show_demo',
        component: () => import('@/components/author_show_demo'),
        meta: { title: '作者详情' },
        hidden: true,
    },
    {
        path: '/paper_show_demo',
        name: 'paper_show_demo',
        component: () => import('@/components/paper_show_demo'),
        meta: { title: '论文详情' },
        hidden: true,
    },
    {
        path: '/ordinarySearch_demo',
        name: 'ordinarySearch_demo',
        component: () => import('@/components/ordinarySearch_demo'),
        meta: { title: '检索' },
        hidden: true,
    },
    {
        path: '/advancedSearch_demo',
        name: 'advancedSearch_demo',
        component: () => import('@/components/advancedSearch_demo'),
        meta: { title: '高级检索' },
        hidden: true,
    },
    {
        path: '/homepage_demo',
        name: 'homepage_demo',
        component: () => import('@/components/homepage_demo'),
        meta: { title: '首页' },
        hidden: true,
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('@/components/login'),
        meta: { title: '登录' },
        hidden: true,
    },
    {
        path: '/register',
        name: 'register',
        component: () => import('@/components/register'),
        meta: { title: '注册' },
        hidden: true,
    },
    {
        path: '/403',
        name: 'e403',
        component: () => import('@/components/error_pages/e403'),
        meta: { title: '403错误' },
        hidden: true,
    },
    {
        path: '/404',
        name: 'e404',
        component: () => import('@/components/error_pages/e404'),
        meta: { title: '404错误' },
        hidden: true,
    },
    {
        path: '/',
        component: () => import('@/components/layout'),
        name: 'layout',
        redirect: '/homepage',
        meta: { role: ['user'], requireAuth: true },
        children: [
            {
                path: '/edit_user_profile',
                component: () => import('@/components/edit_user_profile'),
                name: 'edit_user_profile',
                meta: {
                    title: '编辑个人信息',
                    role: ['user'],
                    requireAuth: true,
                    hidden: true,
                    keepAlive: true // 标记子页面需要缓存
                },
            },
            
            {
                path: '/homepage',
                component: () => import('@/components/homepage'),
                name: 'homepage',
                meta: {
                    title: '主页',
                    role: ['user'],
                    requireAuth: true,
                    keepAlive: true // 标记子页面需要缓存
                },
            },
            {
                path: '/author_info/',
                component: () => import('@/components/author_info'),
                name: 'author_info',
                meta: {
                    title: '作者个人中心',
                    role: ['user'],
                    requireAuth: true,
                    keepAlive: false // 标记子页面需要缓存
                },
            },
            {
                path: '/author_profile/',
                component: () => import('@/components/author_profile'),
                name: 'author_profile',
                meta: {
                    title: '作者信息',
                    role: ['user'],
                    requireAuth: true,
                    hidden: true,
                    keepAlive: false // 标记子页面需要缓存
                },
            },
            {
                path: '/search_author',
                component: () => import('@/components/author_search'),
                name: 'search_author',
                meta: {
                    title: '搜索作者',
                    role: ['user'],
                    requireAuth: true,
                    hidden: true,
                    keepAlive: true // 标记子页面需要缓存
                },
            },
            {
                path: '/favorite',
                component: () => import('@/components/favorite'),
                name: 'favorite',
                meta: {
                    title: '收藏',
                    role: ['user'],
                    requireAuth: true,
                    hidden: true,
                    keepAlive: false // 标记子页面需要缓存
                },
            },
            {
                path: '/interest',
                component: () => import('@/components/interest_author'),
                name: 'interest',
                meta: {
                    title: '关注作者',
                    role: ['user'],
                    requireAuth: true,
                    hidden: true,
                    keepAlive: false // 标记子页面需要缓存
                },
            },
            {
                path: '/about',
                component: () => import('@/components/about'),
                name: 'about',
                meta: {
                    title: '关于',
                    role: ['user'],
                    requireAuth: true,
                    hidden: true,
                    keepAlive: false // 标记子页面需要缓存
                },
            },
            {
                path: '/similar_paper/',
                component: () => import('@/components/similar_paper'),
                name: 'similar_paper',
                meta: {
                    title: '相似论文',
                    role: ['user'],
                    requireAuth: true,
                    hidden: true,
                    keepAlive: false // 标记子页面需要缓存
                },
            },
        ],
    },
    {
        path: '/:catchAll(.*)',
        redirect: '/404',
    },
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

/**
 * 路由守卫
 */
router.beforeEach((to, from, next) => {
    NProgress.start()
    if (to.meta.title) {
        // 在router中设置的页面标题
        document.title = to.meta.title + '-' + process.env.VUE_APP_TITLE
    }
    const token = store.state.token
    const role = store.state.role
    if (to.meta.requireAuth) {
        if (!token) {
            notification['warning']({
                message: '需要登录',
                description: '正在跳转到登录页面。',
            })
            next('/login')
        }
        if (!to.meta.role || to.meta.role.indexOf(role) >= 0) {
            next()
        } else {
            // 403时清除登录信息
            if (!store.state.role) {
                store.dispatch('UPDATE_USER_INFO')
            }
            next('/403')
        }
    } else {
        if (to.path === '/login' && token && !store.state.logout) {
            next('/')
        } else {
            next()
        }
    }
})

router.afterEach((to, from) => {
    // log.info(`Router: '${from.fullPath}' --> '${to.fullPath}'`)
    NProgress.done()
})
export default router
